%% Fidrmuc J., Kapounek, S., Ku�erov�, Z: Time�Frequency Analysis of Cryptocurrency Attention
% wavelet coherency: cryptocurrencies vs. SP 500, EPU

%% Figure 4 - coherency: sp500 vs. returns - kraken
close all
clear
clc
cd('... path ...')
load('sp500.mat')

% transformation and variable definition
datelabel=datelabel1(2:end,:);
X(:,1)=btc_kraken1;
X(:,2)=eth_kraken1;
X(:,3)=ltc_kraken1;
X=diff(X,1,1);
Y(:,1)=sp1;
Y(:,2)=sp1;
Y(:,3)=sp1;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
names={'Bitcoin', 'Ethereum', 'Litecoin'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';

% ticks
ticks1=210:600:length(period1);
ticks2=150:440:length(period2);
ticks3=200:600:length(period3);

% figure creation
close all
figure('units','pixels','Position',[50,100,900,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

% size
s=get(s1, 'position');s(1)=0.05;s(3)=0.26;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.36;s(3)=0.26;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.67;s(3)=0.26;set(s3,'position',s);

mkdir('results_final');
cd('results_final');
print(gcf,'fig04','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A2a - coherency: sp500 vs. returns - bitcoin
close all
clear
clc
cd('... path ...')
load('sp500.mat')

% transformation and variable definition
datelabel=datelabel1(2:end,:);
X(:,1)=btc_bitfinex1;
X(:,2)=btc_bitstamp1;
X(:,3)=btc_bittrex1;
X(:,4)=btc_coinbase1;
X(:,5)=btc_kraken1;
X=diff(X,1,1);
Y(:,1)=sp1;
Y(:,2)=sp1;
Y(:,3)=sp1;
Y(:,4)=sp1;
Y(:,5)=sp1;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='bitcoin';
Cname='Bitcoin';
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=150:500:length(period1);
ticks2=160:510:length(period2);
ticks3=120:410:length(period3);
ticks4=160:520:length(period4);
ticks5=210:600:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA02a','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA02a_bitcoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A02b - coherency: sp500 vs. returns - ethereum
close all
clear
clc
cd('... path ...')
load('sp500.mat')

% transformation and variable definition
datelabel=datelabel1(2:end,:);
X(:,1)=eth_bitfinex1;
X(:,2)=eth_bitstamp1;
X(:,3)=eth_bittrex1;
X(:,4)=eth_coinbase1;
X(:,5)=eth_kraken1;
X=diff(X,1,1);
Y(:,1)=sp1;
Y(:,2)=sp1;
Y(:,3)=sp1;
Y(:,4)=sp1;
Y(:,5)=sp1;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='ethereum';
Cname='Ethereum';
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=150:370:length(period1);
ticks2=80:210:length(period2);
ticks3=100:270:length(period3);
ticks4=140:345:length(period4);
ticks5=150:440:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA02b','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA02b_ethereum','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A02c - coherency: sp500 vs. returns - litecoin
close all
clear
clc
cd('... path ...')
load('sp500.mat')

% transformation and variable definition
datelabel=datelabel1(2:end,:);
X(:,1)=ltc_bitfinex1;
X(:,2)=ltc_bitstamp1;
X(:,3)=ltc_bittrex1;
X(:,4)=ltc_coinbase1;
X(:,5)=ltc_kraken1;
X=diff(X,1,1);
Y(:,1)=sp1;
Y(:,2)=sp1;
Y(:,3)=sp1;
Y(:,4)=sp1;
Y(:,5)=sp1;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
cname='litecoin';
Cname='Litecoin';
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=145:480:length(period1);
ticks2=90:250:length(period2);
ticks3=90:245:length(period3);
ticks4=125:330:length(period4);
ticks5=200:600:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA02c','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA02c_litecoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure 5 - coherency: epu vs. returns - kraken
close all
clear
clc
cd('... path ...')
load('epu.mat')

% transformation and variable definition
datelabel=datelabel2(2:end,:);
X(:,1)=btc_kraken2;
X(:,2)=eth_kraken2;
X(:,3)=ltc_kraken2;
X=diff(X,1,1);
Y(:,1)=epu2;
Y(:,2)=epu2;
Y(:,3)=epu2;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
names={'Bitcoin', 'Ethereum', 'Litecoin'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';

% ticks
ticks1=310:890:length(period1);
ticks2=250:610:length(period2);
ticks3=320:850:length(period3);

% figure creation
close all
figure('units','pixels','Position',[50,100,900,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

% size
s=get(s1, 'position');s(1)=0.05;s(3)=0.26;set(s1,'position',s); % [left bottom width height]
s=get(s2, 'position');s(1)=0.36;s(3)=0.26;set(s2,'position',s);
s=get(s3, 'position');s(1)=0.67;s(3)=0.26;set(s3,'position',s);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'fig05','png');
print(gcf,'fig05','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A3a - coherency: epu vs. returns - bitcoin
close all
clear
clc
cd('... path ...')
load('epu.mat')

% transformation and variable definition
datelabel=datelabel2(2:end,:);
X(:,1)=btc_bitfinex2;
X(:,2)=btc_bitstamp2;
X(:,3)=btc_bittrex2;
X(:,4)=btc_coinbase2;
X(:,5)=btc_kraken2;
X=diff(X,1,1);
Y(:,1)=epu2;
Y(:,2)=epu2;
Y(:,3)=epu2;
Y(:,4)=epu2;
Y(:,5)=epu2;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=250:700:length(period1);
ticks2=250:720:length(period2);
ticks3=200:590:length(period3);
ticks4=270:710:length(period4);
ticks5=310:890:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA03a','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA03a_bitcoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A03b - coherency: epu vs. returns - ethereum
close all
clear
clc
cd('... path ...')
load('epu.mat')

% transformation and variable definition
datelabel=datelabel2(2:end,:);
X(:,1)=eth_bitfinex2;
X(:,2)=eth_bitstamp2;
X(:,3)=eth_bittrex2;
X(:,4)=eth_coinbase2;
X(:,5)=eth_kraken2;
X=diff(X,1,1);
Y(:,1)=epu2;
Y(:,2)=epu2;
Y(:,3)=epu2;
Y(:,4)=epu2;
Y(:,5)=epu2;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=200:520:length(period1);
ticks2=120:320:length(period2);
ticks3=150:390:length(period3);
ticks4=200:500:length(period4);
ticks5=250:610:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA03b','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA03b_ethereum','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);

%% Figure A03c - coherency: epu vs. returns - litecoin
close all
clear
clc
cd('... path ...')
load('epu.mat')

% transformation and variable definition
datelabel=datelabel2(2:end,:);
X(:,1)=ltc_bitfinex2;
X(:,2)=ltc_bitstamp2;
X(:,3)=ltc_bittrex2;
X(:,4)=ltc_coinbase2;
X(:,5)=ltc_kraken2;
X=diff(X,1,1);
Y(:,1)=epu2;
Y(:,2)=epu2;
Y(:,3)=epu2;
Y(:,4)=epu2;
Y(:,5)=epu2;
Y=diff(Y,1,1);
clearvars -except X Y datelabel names 
names={'Bitfinex', 'Bitstamp', 'Bittrex', 'Coinbase', 'Kraken'};

% NaN values
A1=[Y(:,1) X(:,1)];
A2=[Y(:,2) X(:,2)];
A3=[Y(:,3) X(:,3)];
A4=[Y(:,4) X(:,4)];
A5=[Y(:,5) X(:,5)];
B1=A1(~any(isnan(A1),2),:);datelabel1=datelabel(~any(isnan(A1),2),:);
B2=A2(~any(isnan(A2),2),:);datelabel2=datelabel(~any(isnan(A2),2),:);
B3=A3(~any(isnan(A3),2),:);datelabel3=datelabel(~any(isnan(A3),2),:);
B4=A4(~any(isnan(A4),2),:);datelabel4=datelabel(~any(isnan(A4),2),:);
B5=A5(~any(isnan(A5),2),:);datelabel5=datelabel(~any(isnan(A5),2),:);
clear datelabel X Y A*

% period definition
period1=(1:1:(length(datelabel1)))';
period2=(1:1:(length(datelabel2)))';
period3=(1:1:(length(datelabel3)))';
period4=(1:1:(length(datelabel4)))';
period5=(1:1:(length(datelabel5)))';

% ticks
ticks1=230:690:length(period1);
ticks2=130:380:length(period2);
ticks3=130:370:length(period3);
ticks4=190:480:length(period4);
ticks5=320:850:length(period5);

% figure creation
close all
figure('units','pixels','Position',[50,100,1500,300]);
s1=subplot(1,5,1);
wtc([period1 B1(:,1)],[period1 B1(:,2)],'maxscale',800)
colorbar('off');
s=get(s1, 'position');s(1)=0.04;s(3)=0.16;set(s1,'position',s); % [left bottom width height]
title({[cell2mat(names(1))]});
ylabel('Period (days)');
cmap=colormap(jet);
ticklabels=datelabel1(ticks1);set(gca,'XTick',ticks1);set(gca,'XTickLabel',ticklabels);

s2=subplot(1,5,2);
wtc([period2 B2(:,1)],[period2 B2(:,2)],'maxscale',800)
colorbar('off');
s=get(s2, 'position');s(1)=0.225;s(3)=0.16;set(s2,'position',s);
title({[cell2mat(names(2))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel2(ticks2);set(gca,'XTick',ticks2);set(gca,'XTickLabel',ticklabels);

s3=subplot(1,5,3);
wtc([period3 B3(:,1)],[period3 B3(:,2)],'maxscale',800)
colorbar('off');
s=get(s3, 'position');s(1)=0.41;s(3)=0.16;set(s3,'position',s);
title({[cell2mat(names(3))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel3(ticks3);set(gca,'XTick',ticks3);set(gca,'XTickLabel',ticklabels);

s4=subplot(1,5,4);
wtc([period4 B4(:,1)],[period4 B4(:,2)],'maxscale',800)
colorbar('off');
s=get(s4, 'position');s(1)=0.595;s(3)=0.16;set(s4,'position',s);
title({[cell2mat(names(4))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel4(ticks4);set(gca,'XTick',ticks4);set(gca,'XTickLabel',ticklabels);

s5=subplot(1,5,5);
wtc([period5 B5(:,1)],[period5 B5(:,2)],'maxscale',800)
s=get(s5, 'position');s(1)=0.78;s(3)=0.16;set(s5,'position',s);
title({[cell2mat(names(5))]});
ylabel(' ');
cmap=colormap(jet);
ticklabels=datelabel5(ticks5);set(gca,'XTick',ticks5);set(gca,'XTickLabel',ticklabels);

mkdir('results_final');
cd('results_final');
%saveas(gcf,'figA03c','png');
h=gcf;
set(h,'PaperOrientation','landscape');
print(gcf,'figA03c_litecoin','-dpdf','-bestfit','-r600');
cd('..');
close(gcf);





